﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ATest.Bll;
using System.Xml.Linq;

namespace ATest.Tests.ModelTests
{
    /// <summary>
    /// Summary description for QuestionTest
    /// </summary>
    [TestClass]
    public class QuestionTest
    {
        private Question GetValidQuestion()
        {
            var tmp = new Question()
            {
                Name = "Question Name",
                TestId = 1,
            };
            tmp.SetText("Text");
            return tmp;
        }

        [TestMethod]
        public void Validation_ValidQuestion_ReturnsZeroRuleValidations()
        {
            var question = this.GetValidQuestion();
            Assert.AreEqual(0, question.GetRuleViolations().Count());
        }

        [TestMethod]
        public void Validation_RequriesName()
        {
            var question = this.GetValidQuestion();
            question.Name = null;
            Assert.IsFalse(question.IsValid);
            Assert.AreEqual(1, question.GetRuleViolations().Count());
            Assert.AreEqual("Name", question.GetRuleViolations().First().PropertyName);

            question.Name = "";
            Assert.IsFalse(question.IsValid);
            Assert.AreEqual(1, question.GetRuleViolations().Count());
            Assert.AreEqual("Name", question.GetRuleViolations().First().PropertyName);

            question.Name = "a";
            Assert.IsTrue(question.IsValid);
            Assert.AreEqual(0, question.GetRuleViolations().Count());
        }

        [TestMethod]
        public void Validation_RequriesText()
        {
            var question = this.GetValidQuestion();
            question.SetText(null);
            Assert.IsFalse(question.IsValid);
            Assert.AreEqual(1, question.GetRuleViolations().Count());
            Assert.AreEqual("Text", question.GetRuleViolations().First().PropertyName);

            question.SetText("");
            Assert.IsFalse(question.IsValid);
            Assert.AreEqual(1, question.GetRuleViolations().Count());
            Assert.AreEqual("Text", question.GetRuleViolations().First().PropertyName);

            question.SetText("test");
            Assert.IsTrue(question.IsValid);
            Assert.AreEqual(0, question.GetRuleViolations().Count());

        }

        [TestMethod]
        public void SetText_ToStringValue()
        {
            var question = new Question();
            question.SetText("test");

            Assert.AreEqual("test", question.GetText());
            Assert.AreEqual("xml", question.Text.Name);
        }

        [TestMethod]
        public void SetText_ToHtml()
        {
            var question = new Question();
            question.SetText("<p>Para1</p><p>Para2</p>");

            Assert.AreEqual("Para1Para2", question.Text.Value);
            Assert.AreEqual("xml", question.Text.Name);
            Assert.AreEqual(2, question.Text.Elements().Count());
        }

        [TestMethod]
        public void GetText_FromHtml()
        {
            var question = new Question();
            question.SetText("<p>Para1</p><p>Para2</p><img src=\"\" />");

            Assert.AreEqual("Para1Para2", question.Text.Value);

            Assert.AreEqual("<p>Para1</p><p>Para2</p><img src=\"\" />", question.GetText());
        }

        [TestMethod]
        public void GetText_FromNetedHtml()
        {
            var question = new Question();
            question.SetText("<p>Para1</p><p>Para2<ul><li>item</li></ul></p><img src=\"\" />");

            Assert.AreEqual(3, question.Text.Elements().Count());
            Assert.AreEqual("Para1Para2item", question.Text.Value);

            Assert.AreEqual("<p>Para1</p><p>Para2<ul><li>item</li></ul></p><img src=\"\" />", question.GetText());
        }

        [TestMethod]
        public void GetTextSummart_ReturnsCorrectString()
        {
            var question = new Question();
            var tmpString = new StringBuilder();
            while(tmpString.Length < 1000)
            {
                tmpString.Append("<p class=\"testing\">test</p><p>Test<ul><li>1</li><li>2</li></ul></p>");
            }
            question.SetText(tmpString.ToString());
            var result = question.GetTextSummary(20);

            Assert.IsTrue(result.StartsWith("<p class=\"testing\">test</p>"));
            var tmpXElement = XElement.Parse(string.Concat("<xml>", result, "</xml>"));
            Assert.AreEqual(20, tmpXElement.Value.Length);

            result = question.GetTextSummary(2000);
            Assert.IsTrue(result.StartsWith("<p class=\"testing\">test</p>"));
            tmpXElement = XElement.Parse(string.Concat("<xml>", result, "</xml>"));
            Assert.AreEqual(tmpString.ToString(), result);
        }
    }
}
